﻿using Hangfire;
using Hangfire.Storage;
using IService.Tasks;

namespace HangfireTask
{
    public class HangfireDispose
    {
        #region 配置服务

        public static void HangfireService()
        {
            //Fire - And - forget（发布 / 订阅）
            //这是一个主要的后台任务类型，持久化消息队列会去处理这个任务。当你创建了一个发布 / 订阅任务，该任务会被保存到默认队列里面（默认队列是"Default"，但是支持使用多队列）。多个专注的工作者（Worker）会监听这个队列，并且从中获取任务并且完成任务。
            //BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget"));

            //延迟
            //如果想要延迟某些任务的执行，可以是用以下任务。在给定延迟时间后，任务会被排入队列，并且和发布 / 订阅任务一样执行。
            //BackgroundJob.Schedule(() => Console.WriteLine("Delayed"), TimeSpan.FromDays(1));

            //循环
            //按照周期性（小时，天等）来调用方法，请使用RecurringJob类。在复杂的场景，您可以使用CRON表达式指定计划时间来处理任务。
            //RecurringJob.AddOrUpdate(() => Console.WriteLine("Daily Job"), Cron.Daily);

            //连续
            //连续性允许您通过将多个后台任务链接在一起来定义复杂的工作流。
            //var id = BackgroundJob.Enqueue(() => Console.WriteLine("Hello, "));
            //BackgroundJob.ContinueWith(id, () => Console.WriteLine("world!"));

            //这里呢就是需要触发的方法 "0/10 * * * * ? " 可以自行搜索cron表达式 代表循环的规律很简单
            //CancelOrderJob代表你要触发的类 Execute代表你要触发的方法

            using var connection = JobStorage.Current.GetConnection();
            if (connection is JobStorageConnection storageConnection)
            {
                //初始化Redis数据
               // BackgroundJob.Enqueue<InitRedisReserve>(s => s.Execute());

                //更新Redis 数据
                RecurringJob.AddOrUpdate<IHangFireService>("AutoBuildRedis", s => s.AutoBuildRedis(), Cron.Monthly);
                //自动发送提醒消息
                RecurringJob.AddOrUpdate<IHangFireService>("AutoSendMessage", s => s.AutoSendMessage(), Cron.Minutely);
                ////自动执行优惠券发送任务
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoDoCouponTask", s => s.AutoDoCouponTask(), Cron.Minutely);
                ////自动激活，过期优惠券
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoChangeCouponStatus", s => s.AutoChangeCouponStatus(), Cron.Minutely);
                ////自动激活，过期用户优惠券
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoChangeCustomerCouponStatus", s => s.AutoChangeCustomerCouponStatus(), Cron.Minutely);

                ////自动过期或置顶作品
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoActionArtTop", s => s.AutoActionArtTop(), Cron.Minutely);
                ////自动过期或置顶教程
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoActionTutorialTop", s => s.AutoActionTutorialTop(), Cron.Minutely);
                ////自动过期或置顶商品
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoActionGoodTop", s => s.AutoActionGoodTop(), Cron.Minutely);
                ////自动开始投票
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoActionVote", s => s.AutoActionVote(), Cron.Minutely);

                ////投票开始后发奖励
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoActionVoteRewards", s => s.AutoActionVoteRewards(), Cron.Hourly);

                ////计算排名提示名次下降的作者
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoActionVoteRank", s => s.AutoActionVoteRank(), Cron.Hourly);

                ////自动结算投票结果
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoActionVoteResult", s => s.AutoActionVoteResult(), Cron.Minutely);

                ////自动去虚拟评论
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoVirturalComment", s => s.AutoVirturalComment(), Cron.Hourly);

                ////序列号库存告罄
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoNoticeGoodCodeNull", s => s.AutoNoticeGoodCodeNull(), Cron.Daily);
                ////实体商品规格库存告罄
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoNoticeGoodSkusNull", s => s.AutoNoticeGoodSkusNull(), Cron.Daily);

                ////自动过期兑换券
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoActionVoucher", s => s.AutoActionVoucher(), Cron.Hourly);

                ////自动执行勋章发送任务
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoDoCustomerMedal", s => s.AutoDoCustomerMedal(), Cron.Minutely);

                //RecurringJob.AddOrUpdate<IHangFireService>("AutoChangeVirtualItemStatus", s => s.AutoChangeVirtualItemStatus(), Cron.Minutely);

                ////RecurringJob.AddOrUpdate<IHangFireService>("AutoGetDashboard", s => s.AutoGetDashboard(), Cron.Hourly);
                ////RecurringJob.AddOrUpdate<IHangFireService>("AutoGetOrderAnalysis", s => s.AutoGetOrderAnalysis(), Cron.Hourly);
                ////RecurringJob.AddOrUpdate<IHangFireService>("AutoGetTrendAnalysis", s => s.AutoGetTrendAnalysis(), Cron.Hourly);

                //RecurringJob.AddOrUpdate<IHangFireService>("AutoGetDashboard", s => s.AutoGetDashboard(), "*/10 * * * *");
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoGetOrderAnalysis", s => s.AutoGetOrderAnalysis(), "*/10 * * * *");
                //RecurringJob.AddOrUpdate<IHangFireService>("AutoGetTrendAnalysis", s => s.AutoGetTrendAnalysis(), "*/10 * * * *");

                //RecurringJob.AddOrUpdate<IHangFireService>("AutoClearData", s => s.AutoClearData(), Cron.Daily);
            }
        }

        #endregion 配置服务
    }
}